<?php

namespace App\Admin\Forms;

use App\Models\Order\GoodsOrder;
use App\Models\Order\OrderSample;
use App\Models\User\FamilyUser;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\Log;

class OrderSampleReport extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;
        $orderSample = OrderSample::find($id);
        $readonly = $orderSample->status == 3 ? false : true;
        if (!$readonly) {
            $checkItem = $orderSample->checkItem;
            $parameters = $checkItem->parameters;
            foreach ($parameters as &$parameter) {
                $orderSample->parameters()->updateOrCreate([
                    'item_id' => $checkItem->id,
                    'parameter_id' => $parameter->id,
                ], [
                    'value' => $input[$parameter->aliasname],
                ]);
            }
            $orderSample->check_pdf_id = $input['check_pdf_id'] ?? 0;
            if ($input['is_pass']) {
                $orderSample->status = 4;
            }
            $orderSample->save();

            $goodsOrder = $orderSample->goodsOrder;
            $samples = $goodsOrder->samples;
            $flag = true;
            $samples->each(function ($sample) use (&$flag) {
                if ($sample->status != 4) {
                    $flag = false;
                }
            });
            if ($flag) {
                $goodsOrder->status = 4;
                $goodsOrder->save();
            }
            return $this
                ->response()
                ->success('保存成功')
                ->refresh();
        } else {
            return $this
                ->response()
                ->success('关闭');
        }
    }

    public function form()
    {
        $id = $this->payload['id'] ?? null;
        $orderSample = OrderSample::find($id);
        $readonly = $orderSample->status == 3 ? false : true;
        $checkItem = $orderSample->checkItem;
        $parameters = $checkItem->parameters;
        foreach ($parameters as &$parameter) {
            $parameterValue = $orderSample->parameters()->where('parameter_id', $parameter->id)->first();
            if ($parameter->type_id == 1) {
                $value = $parameterValue ? $parameterValue->value : '';
                $this->text($parameter->aliasname, $parameter->name)->readOnly($readonly)->value($value);
            } else if ($parameter->type_id == 2) {
                $value = $parameterValue ? $parameterValue->value : '0';
                $this->text($parameter->aliasname, $parameter->name)->readOnly($readonly)->type('number')->value($value);
            } else if ($parameter->type_id == 3) {
                $value = $parameterValue ? $parameterValue->value : '';
                $this->textarea($parameter->aliasname, $parameter->name)->readOnly($readonly)->value($value);
            }
        }
        if (!$readonly) {
            $this->uploadfilepro('check_pdf_id', '检测报告')->url('upload-file')->autoUpload()->value($orderSample->check_pdf_id)->readOnly($readonly)->render();
            $this->switch('is_pass', '是否通过')->value(0);
        }
    }
}
